#!/usr/bin/env python3
"""
Script to rename Markdown documents from ./docs directory to txt format
and save them in ./txt directory.
"""

import os
import shutil
from pathlib import Path

def rename_md_to_txt():
    """
    Main function to rename Markdown documents from ./docs to ./txt with txt extension
    """
    # Define directories
    docs_dir = Path('./docs')
    output_dir = Path('./txt')
    
    # Check if source directory exists
    if not docs_dir.exists():
        print(f"Source directory {docs_dir} does not exist")
        return
    
    # Create output directory if it doesn't exist
    output_dir.mkdir(parents=True, exist_ok=True)
    
    # Counter for renamed files
    renamed_count = 0
    
    # Process all .md files in docs directory
    for file_path in docs_dir.iterdir():
        if file_path.is_file() and file_path.suffix.lower() == '.md':
            # Generate output path with .txt extension
            output_path = output_dir / f"{file_path.stem}.txt"
            
            # Copy file with new extension
            try:
                shutil.copy2(file_path, output_path)
                print(f"Renamed {file_path} to {output_path}")
                renamed_count += 1
            except Exception as e:
                print(f"Error renaming {file_path}: {e}")
    
    print(f"Renaming complete. {renamed_count} Markdown files renamed to txt format.")

if __name__ == "__main__":
    rename_md_to_txt()